package com.kevin.learn.demo;

import java.util.List;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;

public class SqlRecover {

    private static String entrepotName = "bn";
    private static String tableName = "commerce_evaluate";
    private static Integer tableLine = 23;
    private static String  fileName ="commerce_evaluate.log";

    public static void main(String[] args) {
        
        int count = 0;
        //默认UTF-8编码，可以在构造中传入第二个参数做为编码
        FileReader fileReader = new FileReader("C:\\Users\\admin_bn\\Desktop\\备份\\"+fileName);
        List<String> list = fileReader.readLines();
        for (int i = 0; i < list.size(); i++) {

            if (list.get(i).contains("DELETE FROM `bn`.`"+tableName+"`")) {
                StringBuilder builder = new StringBuilder();
                builder.append("INSERT INTO ");
                builder.append("`").append(entrepotName).append("`").append(".");
                builder.append("`").append(tableName).append("`").append("VALUES").append("(");
                for (int j = i + 2; j < i + tableLine; j++) {

                    int indexStart = list.get(j).indexOf("=") + 1;
                    int indexEnd = list.get(j).indexOf("/");

                    String substring = list.get(j).substring(indexStart, indexEnd);

                    builder.append(substring);
                    if (j != (i + tableLine - 1)) {
                        builder.append(",");
                    }
                }
                builder.append(");");
                FileWriter writer = new FileWriter("C:\\Users\\admin_bn\\Desktop\\备份\\副本_"+fileName);
                writer.append(builder.toString());
                writer.append("\r\n");
                count++;
                System.out.println(count);
            }
        }
    }
}
